package controller.Supper;

import pojo.Superuser;
import service.Supper.SuperuserService;
import service.Supper.impl.SuperuserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 管理员Servlet
 */
@WebServlet(name = "SuperuserServlet",urlPatterns = "/doUser")
public class SuperuserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        SuperuserService sus = new SuperuserServiceImpl();
        String action = request.getParameter("action");
        if(action ==null || action ==""){
            action ="userLogin";
        }
        if("userLogin".equals(action)){    //验证用户名和密码
            String userLoginName = request.getParameter("userLoginName");
            String userPassword = request.getParameter("userPassword");
            //验证用户名密码是否正确
            Superuser superuser = sus.CheckSuser(userLoginName,userPassword);
            boolean flag = false;
            if(superuser.getUserLoginName()!=null && superuser.getUserPassword()!=null){
                flag = true;
                request.getSession().setAttribute("User",superuser);
            }else{
                flag = false;
            }
            String result="{\"flag\":\""+flag+"\"}";
            out.write(result);
        }else if("QueryById".equals(action)){   //查询个人信息
            Long id = Long.valueOf(request.getParameter("id"));
            Superuser superuser = sus.SelSuser(id);
            request.getSession().setAttribute("User",superuser);
            response.sendRedirect(request.getContextPath()+"/page/user/userQuery.jsp");
        }else if("userUpdate".equals(action)){    //修改个人信息
            //得到session中的数据并进行修改
            Superuser sup= (Superuser) request.getSession().getAttribute("User");
            sup.setId( Long.valueOf(request.getParameter("id")));
            sup.setUserName(request.getParameter("userName"));
            sup.setUserLoginName(request.getParameter("userLoginName"));
            sup.setUserId(request.getParameter("userId"));
            int n = sus.updateSuser(sup);
            boolean flag = false;
            if(n>0){
                flag = true;
                //再次进行存储
                request.getSession().setAttribute("User",sup);
                request.getRequestDispatcher("/page/user/userQuery.jsp?flag="+flag).forward(request,response);
            }else{
                request.getRequestDispatcher("/page/user/userUpdate.jsp?flag="+flag).forward(request,response);
            }
        }else if("userUpdatePwd".equals(action)){     //修改密码
            //得到session中的数据并进行修改
            Superuser sup= (Superuser) request.getSession().getAttribute("User");
            sup.setUserPassword(request.getParameter("userPassword"));
            int n = sus.updateSuserPwd(sup);
            boolean flag = false;
            if(n>0){
                flag = true;
                request.getSession().setAttribute("User",sup);
                request.getRequestDispatcher("/page/user/userQuery.jsp?flag="+flag).forward(request,response);
            }else{
                flag = false;
                request.getRequestDispatcher("/page/user/userUpdate.jsp?flag="+flag).forward(request,response);
            }
        }else if("out".equals(action)){    //注销
            request.getSession().invalidate();
            response.sendRedirect(request.getContextPath()+"/Login.jsp");
        }
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
